package com.hh.vietnameseproject.ui.read

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hh.vietnameseproject.R
import com.hh.vietnameseproject.logic.model.ReadList
import com.hh.vietnameseproject.logic.network.AllNetWork
import com.just.agentweb.AgentWeb
import com.just.agentweb.DefaultWebClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject
import java.util.ArrayList

class ReadFragment:Fragment() {

    lateinit var webViewContainer:LinearLayoutCompat
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //设置通知栏透明
//        val activity = context as Activity
//        val decorView: View = activity.window.decorView
//        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
//        activity.window.statusBarColor = Color.TRANSPARENT
        val view = inflater.inflate(R.layout.read_fragment,container,false)
        val recyclerView = view.findViewById<RecyclerView>(R.id.readRecyclerView)
        recyclerView.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)

        GlobalScope.launch {
            val body = AllNetWork.getWebsite().string()
            val jsonObject = JSONObject(body)
            val array = jsonObject.getJSONArray("data")
            withContext(Dispatchers.Main){
                if(jsonObject.getInt("code")==1){
                    val list = ArrayList<ReadList>()
                    for(i in 0 until array.length()){
                        val item = array.getJSONObject(i)
                        list.add(ReadList(item.getString("webname"),item.getString("weburl")))

                    }
                    recyclerView.adapter = ReadItemAdapter(context,list)
                }
            }
        }



        return view
    }



}